package com.atguigu.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Aspect
public class LogAspect {

    @Pointcut("execution(public int com.atguigu.aop.MathCalc.*(..))")
    public void pointCut(){}

    @Before("execution(public int com.atguigu.aop.MathCalc.*(..))")
    public void logStart(JoinPoint joinPoint) {

        System.out.println(joinPoint.getSignature().getName() +
                "开始运行，参数列表{"+
                Arrays.asList(joinPoint.getArgs()) +"}");
    }

    @After("execution(public int com.atguigu.aop.MathCalc.*(..))")
    public void logEnd() {
        System.out.println("运行结束");
    }

    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(Object result) {
        System.out.println("正常返回，运行结果{"+ result +"}");
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void logException(JoinPoint joinPoint, Exception exception) {

        //System.out.println(joinPoint.getSourceLocation().getFileName());

        System.out.println(joinPoint.getSignature().getDeclaringType());
        System.out.println(joinPoint.getSignature().getDeclaringTypeName());
        System.out.println(joinPoint.getSignature().getName());

        StringBuilder stringBuilder = new StringBuilder("发生异常，报错信息{" + exception + "}");
        for (StackTraceElement stackTraceElement : exception.getStackTrace()) {
            stringBuilder.append(">>>>>").append(stackTraceElement.toString());
        }
        System.out.println(stringBuilder.toString());

    }
}
